<?php

namespace App\Api\Http\Controllers\V1\In;

use App\Api\Eloquent\UserMessage;
use App\Api\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Api\Eloquent\User;

class MessageController extends Controller{
    /*
     * 获取消息
     */
    public function getMessage(Request $request,$page){
        $user=User::user();
        $a=10;
        $messages=UserMessage::join('messages','messages.id','=','user_messages.message_id')
            ->where('user_messages.user_id','=',$user->id)
            ->orderBy('messages.published_at','desc')
            ->orderBy('user_messages.readed','asc')
            ->select('messages.*','user_messages.readed')
            ->skip($a*$page)
            ->limit($a)
            ->get();
        $id=array();
        foreach($messages as $message){
            $id[]=$message->id;
        }
        UserMessage::whereIn('message_id',$id)->where('user_id','=',$user->id)->update(['readed'=>'1']);
        $lists=view('api.message',compact('messages'))->__toString();
        return $this->my_jsonp($request->input('callback'),compact('lists'));
    }
}